package com.haiyi.software.app;

import android.content.Intent;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.haiyi.software.app.basic.ActivityBasicCharmCityList;
import com.haiyi.software.app.basic.ActivityBasicCharmCityListGetCharm;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListDelegate;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshListView.PullToRefreshListAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class ActivityCharmCityList extends ActivityBasicCharmCityListGetCharm
		implements PullToRefreshListDelegate {

	private PullToRefreshListView mPullRefreshListView;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;
	private TextView textView_myCharm;

	@Override
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_charm_lists);
		initComp();
		getPersonGold();
		requestNetworkData();
	}

	private void initComp() {

		textView_myCharm = (TextView) this.findViewById(R.id.textView_myCharm);
		mPullRefreshListView = (PullToRefreshListView) this
				.findViewById(R.id.pull_refresh_list_charm);

		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(20)).build();
		mPullRefreshListView.setScrollingWhileRefreshingEnabled(false);
		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						String label = DateUtils.formatDateTime(MainApplication
								.getInstance().getApplicationContext(), System
								.currentTimeMillis(),
								DateUtils.FORMAT_SHOW_TIME
										| DateUtils.FORMAT_SHOW_DATE
										| DateUtils.FORMAT_ABBREV_ALL);
						// Update the LastUpdatedLabel
						refreshView.getLoadingLayoutProxy()
								.setLastUpdatedLabel(label);
						// Do work to refresh the list here.
						requestNetworkData();
					}
				});

		mPullRefreshListView
				.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

					@Override
					public void onLastItemVisible() {

						Toast.makeText(
								MainApplication.getInstance()
										.getApplicationContext(), "已经到底部了!",
								Toast.LENGTH_SHORT).show();
					}
				});

	}

	@Override
	public void requestFinish(JsonObject jsonObjectReturn) {
		// TODO 自动生成的方法存根

		if (data != null && data.size() > 0) {

			mPullRefreshListView.setPullToRefreshListDelegate(this);

			mPullRefreshListView.setDataSource(data);

			mPullRefreshListView.setAdapter(null);

			mPullRefreshListView.getAdapterEx().notifyDataSetChanged();

			mPullRefreshListView.onRefreshComplete();

		}

	}

	@Override
	public View bindView(PullToRefreshListAdapter adapter, int position,
			View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;

		if (convertView == null) {

			convertView = adapter.getLayoutInflater().inflate(
					R.layout.item_charm_city, null);
			viewHolder = new ViewHolder();
			viewHolder.item_i_content = (TextView) convertView
					.findViewById(R.id.item_i_content);
			viewHolder.item_i_info_count = (TextView) convertView
					.findViewById(R.id.item_i_info_count);
			viewHolder.item_i_photo = (ImageView) convertView
					.findViewById(R.id.item_i_photo);
			viewHolder.item_i_time = (Button) convertView
					.findViewById(R.id.item_i_time);
			viewHolder.item_i_title = (TextView) convertView
					.findViewById(R.id.item_i_title);
			convertView.setTag(viewHolder);

		} else {

			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.item_i_content
				.setText(data.get(position).get("pdesc") != null ? data
						.get(position).get("pdesc").toString() : "");
		viewHolder.item_i_info_count
				.setText(data.get(position).get("pMval") != null ? data
						.get(position).get("pMval").toString() : "");
		viewHolder.item_i_title
				.setText(data.get(position).get("pname") != null ? data
						.get(position).get("pname").toString() : "");
		imageLoader.displayImage(
				str_server
						+ (data.get(position).get("picpath") != null ? data
								.get(position).get("picpath").toString() : ""),
				viewHolder.item_i_photo, options);
		viewHolder.item_i_time
				.setTag(data.get(position).get("pid") != null ? data
						.get(position).get("pid").toString() : "");

		viewHolder.item_i_time.setOnClickListener(clickListener);

		return convertView;
	};

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			Intent intent = new Intent();

			intent.setClass(getApplicationContext(), ActivityExchanger.class);

			intent.putExtra("pid", v.getTag().toString());

			startActivityForResult(intent, 10012);
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 10012:
			getPersonGold();
			break;

		}

	};

	private static class ViewHolder {
		ImageView item_i_photo;
		TextView item_i_title;
		TextView item_i_content;
		TextView item_i_info_count;
		Button item_i_time;
	}

	@Override
	protected void getMyGoldSuccess(JsonObject myGoldBody) {
		if (myGoldBody != null) {
			textView_myCharm.setText(myGoldBody.get("vgold").getAsString());
		}
	}

	@Override
	protected void requestError() {
		// TODO 自动生成的方法存根

	}

}
